package cn.kimoon.dau_android;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import cn.kimoon.dau_android.Utility.SPUtils;

/**
 * Created by xiangdong on 2016/11/23.
 */

public class MineFragment extends Fragment implements View.OnClickListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        initSubViews(view);
        return view;
    }

    //
    private void initSubViews(View view) {
        Button nameButton = (Button) view.findViewById(R.id.user_name_button);
        nameButton.setText((String) SPUtils.get(getActivity(), MainActivity.DAU_SP_USER_NAME, "Jack"));
        System.out.println(SPUtils.get(getActivity(), MainActivity.DAU_SP_USER_NAME, "Jack"));
        System.out.println(SPUtils.get(getActivity(), MainActivity.DAU_SP_PASSWORD, "Jack"));
        nameButton.setOnClickListener(this);

        LinearLayout layout = (LinearLayout) view.findViewById(R.id.patrol_bg_layout);
        layout.setOnClickListener(this);
    }

    // 点击事件处理
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.user_name_button:
                userNameBtnTapped();
                break;
            case R.id.patrol_bg_layout:
                jumpToPatrol();
                break;
        }
    }

    // 退出按钮点击
    private void userNameBtnTapped() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        AlertDialog dialog = builder.setMessage(R.string.frag_mine_exit_title)
                .setPositiveButton(R.string.frag_mine_exit_sure, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        clearLoginData();
                        jumpToLoginActivity();
                    }
                })
                .setNegativeButton(R.string.frag_mine_exit_cancel, null)
                .create();
        dialog.show();
    }

    // 清空登录数据
    private void clearLoginData() {
        // 极光推送，取消用用户的name绑定为别名(退出之后就不用收到推送)
        JPushInterface.setAlias(getActivity(), "", new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                Log.d("MineFragment", "取消极光推送Alias成功："+ i + "之前的别名：" + set);
            }
        });

        // 清楚自动登录的标记
        SPUtils.put(getActivity(), MainActivity.DAU_SP_AUTO_LOGIN, false);
    }
    // 返回到登录界面
    private void jumpToLoginActivity() {
        startActivity(new Intent(getActivity(), MainActivity.class));
        getActivity().finish();
    }

    // 跳转到巡更
    private void jumpToPatrol() {
        startActivity(new Intent(getActivity(), PatrolActivity.class));
    }
}
